﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "misc_tool.h"

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos
{
    namespace misc_tool
    {

        // 
        // utf8 to wchar
        // 
        int utf8_to_wchar( const char * lpszSrc, int nSrcByteSize, wchar_t * lpszDest, int nDestByteSize, int * lpnDestNeedSize )
        {
            int nRet = 0;

            int need_size = 0;

            if( !lpszSrc || !lpszDest )
            {
                nRet = 1;
            }

            if( 0 == nRet )
            {
                need_size = MultiByteToWideChar( CP_UTF8, 0, lpszSrc, nSrcByteSize, 0, 0 );

                if( 0 == need_size )
                {
                    nRet = 1;
                }
            }

            if( 0 == nRet )
            {
                if( lpnDestNeedSize )
                {
                    *lpnDestNeedSize = need_size;
                }

                if( nDestByteSize < need_size )
                {
                    nRet = 1;
                }
            }

            if( 0 == nRet )
            {
                int ret = MultiByteToWideChar( CP_UTF8, 0, lpszSrc, nSrcByteSize, lpszDest, nDestByteSize );

                if( 0 == ret )
                {
                    nRet = 1;
                }
            }

            return nRet;
        }

        // 
        // wcharto utf8
        // 
        int wchar_to_utf8( const wchar_t * lpszSrc, int nSrcByteSize, char * lpszDest, int nDestByteSize, int * lpnDestNeedSize )
        {
            int nRet = 0;

            int need_size = 0;

            if( !lpszSrc || !lpszDest )
            {
                nRet = 1;
            }

            if( 0 == nRet )
            {
                need_size = WideCharToMultiByte( CP_UTF8, 0, lpszSrc, nSrcByteSize, 0, 0, 0, 0 );

                if( 0 == need_size )
                {
                    nRet = 1;
                }
            }

            if( 0 == nRet )
            {
                if( lpnDestNeedSize )
                {
                    *lpnDestNeedSize = need_size;
                }

                if( nDestByteSize < need_size )
                {
                    nRet = 1;
                }
            }

            if( 0 == nRet )
            {
                int ret = WideCharToMultiByte( CP_UTF8, 0, lpszSrc, nSrcByteSize, lpszDest, nDestByteSize, 0, 0 );

                if( 0 == ret )
                {
                    nRet = 1;
                }
            }

            return nRet;
        }

        // 
        // utf8 to ansi
        // 
        int utf8_to_ansi( const char * lpszSrc, int nSrcByteSize, char * lpszDest, int nDestByteSize, int * lpnDestNeedSize )
        {
            int nRet = 0;

            const int N = 40960;
            wchar_t wTemp[N];
            int nWCharNum = 0;
            int nMCharNum = 0;

            if( !lpszSrc || !lpszDest )
            {
                nRet = 1;
            }

            if( 0 == nRet )
            {
                nWCharNum = MultiByteToWideChar( CP_UTF8, 0, lpszSrc, nSrcByteSize, 0, 0 );
                if( nWCharNum > N )
                {
                    nRet = 1;
                }
            }

            if( 0 == nRet )
            {
                nWCharNum = MultiByteToWideChar( CP_UTF8, 0, lpszSrc, nSrcByteSize, wTemp, N );
            }

            if( 0 == nRet )
            {
                nMCharNum = WideCharToMultiByte( CP_ACP, 0, wTemp, nWCharNum, 0, 0, NULL, NULL );
                if( nMCharNum > nDestByteSize )
                {
                    nRet = 1;
                }
            }

            if( 0 == nRet )
            {
                nMCharNum = WideCharToMultiByte( CP_ACP, 0, wTemp, nWCharNum, lpszDest, nDestByteSize, NULL, NULL );
            }

            if( lpnDestNeedSize )
            {
                *lpnDestNeedSize = nMCharNum;
            }

            return nRet;
        }

        // 
        // ansi utf8
        // 
        int ansi_to_utf8( const char * lpszSrc, int nSrcByteSize, char * lpszDest, int nDestByteSize, int * lpnDestNeedSize )
        {
            int nRet = 0;

            const int N = 40960;
            wchar_t wTemp[N];
            int nWCharNum = 0;
            int nMCharNum = 0;

            if( !lpszSrc || !lpszDest )
            {
                nRet = 1;
            }

            if( 0 == nRet )
            {
                nWCharNum = MultiByteToWideChar( CP_ACP, 0, lpszSrc, nSrcByteSize, 0, 0 );
                if( nWCharNum > N )
                {
                    nRet = 1;
                }
            }

            if( 0 == nRet )
            {
                nWCharNum = MultiByteToWideChar( CP_ACP, 0, lpszSrc, nSrcByteSize, wTemp, N );
            }

            if( 0 == nRet )
            {
                nMCharNum = WideCharToMultiByte( CP_UTF8, 0, wTemp, nWCharNum, 0, 0, NULL, NULL );
                if( nMCharNum > nDestByteSize )
                {
                    nRet = 1;
                }
            }

            if( 0 == nRet )
            {
                nMCharNum = WideCharToMultiByte( CP_UTF8, 0, wTemp, nWCharNum, lpszDest, nDestByteSize, NULL, NULL );
            }

            if( lpnDestNeedSize )
            {
                *lpnDestNeedSize = nMCharNum;
            }

            return nRet;
        }

        const char * change_path_to_linux_style( char * lpszPath )
        {
            char * pStr = lpszPath;

            while( 0 != *pStr )
            {
                if( '\\' == *pStr )
                {
                    *pStr = '/';
                }
                ++pStr;
            }

            return lpszPath;
        }

    } // misc_tool
} // xos
